﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using XBMC.Cache.Manager.Shared;

namespace XBMC.Cache.Manager
{
    public class DBCacheFile
    {
        public enum CacheFileType
        {
            Movie = 1,
            Actor = 2
        }

        private string hashFile = null;
        private string hashFileAndFolder = null;

        [DBField("DBObjectType")]
        public Int64 DBObjectType { get; set; }
        [DBField("ObjectID")]
        public int ObjectID { get; set; }
        [DBField("Filename")]
        public string Filename { get; set; }

        public CacheFileType ObjectType
        {
            get { return (CacheFileType)DBObjectType; }
            set { DBObjectType = (int)value; }
        }

        public string PhysicalFile
        {
            get
            {
                string file = null;

                try
                {
                    if (Filename.ToLower().StartsWith("smb://"))
                    {
                        file = Filename.Substring(4);
                        file = file.Replace("/", Path.DirectorySeparatorChar.ToString());
                    }

                    if (Filename.ToLower().StartsWith("special://masterprofile/thumbnails/"))
                    {
                        file = RegistryHelper.LoadRegistryOption("LocalCacheFolder") + Filename.Substring("special://masterprofile/thumbnails/".Length - 1);
                        file = file.Replace("/", Path.DirectorySeparatorChar.ToString());
                    }
                }
                catch { }

                return file;
            }
        }

        public string CacheHashFile
        {
            get
            {
                switch (DBObjectType)
                {
                    case 100:
                        string file = Filename;
                        if (file.Contains("/"))
                            file = file.Substring(file.LastIndexOf("/") + 1);

                        file = Path.GetFileNameWithoutExtension(file);

                        return file;

                    default:
                        if (hashFile == null)
                        {
                            XBMC xbmc = new XBMC();
                            hashFile = xbmc.XBMCHash(Filename);
                        }
                        return hashFile;
                }
            }
        }

        public string CacheHashFolderAndFile
        {
            get
            {
                if (hashFileAndFolder == null)
                {
                    XBMC xbmc = new XBMC();
                    string xbmcHash = xbmc.XBMCHash(Filename);
                    hashFileAndFolder = xbmcHash.Substring(0, 1) + "\\" + xbmcHash;
                }
                return hashFileAndFolder;
            }
        }
    }
}
